<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14
 * Time: 11:35
 */
//创建Server对象，监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set([
    'worker_num' => 6,//worker进程数
    'max_request' => 10000,
    //'daemonize' => 1 //将转入后台作为守护进程运行
]);
//监听连接进入事件
$serv->on('connect', function ($serv, $fd, $reactor_id) {
    echo "Client: Connect.线程id：{$reactor_id}，客户端id：{$fd}" . PHP_EOL;
});

//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
    $serv->send($fd, "Server: 线程id：{$reactor_id}，客户端id：{$fd}，接收数据：".$data);
});

//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.客户端id：{$fd}" . PHP_EOL;
});

//启动服务器
$serv->start();